﻿using Publicuse.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Autofac
{
    public interface IAutofaceInstance
    {
        /// <summary>
        /// 日志记录统一请求接口
        /// </summary>
        /// <param name="businessType"></param>
        /// <param name="actionType"></param>
        /// <param name="entity"></param>
        /// <param name="method"></param>
        /// <param name="businessLogType"></param>
        /// <param name="func"></param>
        /// <param name="action"></param>
        /// <param name="commonResult"></param>
        /// <returns></returns>
        CommonResult DoWork(BusinessType businessType, BusinessActionType actionType, object entity, string method, BusinessLogType businessLogType,
            Func<BusinessType, CommonResult> func, Func<CommonResult, bool> action, ref CommonResult commonResult);
    }

    public class AutofaceInstance : IAutofaceInstance
    {
        public CommonResult DoWork(BusinessType businessType, BusinessActionType actionType, object entity, string method, BusinessLogType businessLogType,
            Func<BusinessType, CommonResult> func, Func<CommonResult, bool> action, ref CommonResult commonResult)
        {
            try
            {
                commonResult = func.Invoke(businessType);
                if (action != null)
                    action.BeginInvoke(commonResult, null, null);
            }
            catch (Exception ex)
            {
                commonResult.msg = ex.Message;
                commonResult.code = (int)CodeResult.exception;
            }
            return commonResult;
        }
    }
}
